<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:20
 */

namespace App\Admin\Repositories;

use App\Admin\Contracts\ConfigInterface;
use App\Admin\Models\ConfigClassModel;
use App\Admin\Models\ConfigModel;
use Illuminate\Support\Facades\Request;


/**
 * 后台配置实现
 * Class DbConfigRepository
 * @package App\Admin\Repositories
 */
class DbConfigRepository implements ConfigInterface
{

    /**
     * 配置模型
     * @return ConfigModel
     */
    public function getConfigModel()
    {
        return app()->make(ConfigModel::class);
    }

    /**
     * 配置参数分类模型
     * @return ConfigClassModel
     */
    public function getConfigClassModel()
    {
        return app()->make(ConfigClassModel::class);
    }

    /**
     * 根据参数配置获取数据
     * @param $where
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
     */
    public function getConfigClassMenuList($where)
    {
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        return $this->getConfigClassModel()->newQuery()->where($where)->paginate($limit);
    }

    /**
     * 根据参数配置获取数据
     * @param $where
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
     */
    public function getConfigClassMenuListAll($where)
    {

        return $this->getConfigClassModel()->newQuery()->where($where)->get();
    }

    public function getConfigClassMenuListAllId($where){
        return $this->getConfigClassModel()->newQuery()->where($where)->pluck("id");
    }

    /**
     * 获取分类单信息
     * @param $where
     * @return \Illuminate\Database\Eloquent\Model|mixed|null|object|static
     */
    public function getConfigClassInfo($where)
    {
        return $this->getConfigClassModel()->newQuery()->where($where)->first();
    }

    /**
     * 配置添加与更新
     * @param $data
     * @return mixed|void
     */
    public function updateOrCreateConfigClassData($data)
    {
        $data['status']=empty($data["status"])?0:1;
        $id = $data["id"];
        unset($data["id"]);
        $data=$this->getConfigClassModel()->filter($data);
        return $this->getConfigClassModel()->newQuery()->updateOrCreate(["id"=>$id],$data);
    }
    /**
     * 删除分类信息
     * @param $where
     * @return mixed
     */
    public function delConfigClass($where){
        return $this->getConfigClassModel()->newQuery()->where($where)->delete();
    }

    /**
     * 获取配置列表
     * @param $where
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getConfigList($where)
    {

        $request = Request::instance();
        $p = $request->input("page", 1);
        $limit = $request->input("limit", 10);
        $sort = $request->input("sort", "");
        $order = $request->input("order", "");
        return $this->getConfigModel()->newQuery()->where($where)->orderBy($sort, $order)->offset(($p - 1) * $limit)->limit($limit)->get();
    }

    /**
     * 获取配置列表数据
     * @param $where
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getConfigListAll($where){
        $request = Request::instance();
        $sort = $request->input("sort", "sort");
        $order = $request->input("order", "desc");
        return $this->getConfigModel()->newQuery()->select(["id","name","title","value"])->where($where)->orderBy($sort, $order)->get();
    }

    /**
     * 获取配置总数
     * @param $where
     * @return int
     */
    public function getConfigCount($where)
    {
        return $this->getConfigModel()->newQuery()->where($where)->count();
    }

    /**
     * 数据编辑与更新
     * @param $data
     * @return mixed
     */
    public function saveConfigData($data)
    {
        //$data=$this->getConfigModel()->filter($data);
        return $this->getConfigModel()->newQuery()->updateOrCreate(["id" => $data["id"]], $data);
    }

    /**
     * 获取配置单条数据
     * @param $where
     * @return mixed|void
     */
    public function getConfigInfo($where)
    {


        return $this->getConfigModel()->newQuery()->where($where)->first();

    }

    /***
     * 配置参数删除
     * @param $where
     * @return mixed|void
     */
    public function delConfigData($where)
    {
        return $this->getConfigModel()->newQuery()->where($where)->delete();
    }

    /**
     * 设置配置
     * @param $where
     * @param $data
     * @return int
     */
    public function setConfigValue($where,$data){
        return $this->getConfigModel()->newQuery()->where($where)->update($data);
    }

    public function changeStatus($data){
        return $this->getConfigModel()->newQuery()->where("id",$data["id"])->update(["status"=>$data['status']]);
    }

}